package com.gwsdk.paysdk.acitvityV2.fragment;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwsdk.paysdk.acitvityV2.GWMainMenuActivity;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.entity.GamePackInfo;
import com.gwsdk.paysdk.http.process.PacksCodeProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

public class GWGiftDetailsFragment extends GWBaseFragment{

    private GamePackInfo gamePackInfo;

    private TextView giftName;
    private TextView giftNumber;
    private TextView receiveBtn;
    private TextView giftTimer;
    private TextView giftExplain;
    private TextView giftContent;
    private TextView giftWarning;
    private View giftExplainView;
    private View giftWarningView;



    private ImageView backItem;

    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_gift_details");
    }

    @Override
    public void initView(View view) {
        backItem = view.findViewById(GWInflaterUtils.getControl(getActivity(),"btn_back"));
        backItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GWMainMenuActivity activity = (GWMainMenuActivity) getActivity();
                activity.hideOtherFragment(GWGiftDetailsFragment.this);
            }
        });

        giftName = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_name"));
        giftNumber = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_number"));
        receiveBtn = view.findViewById(GWInflaterUtils.getControl(getActivity(),"receive_btn"));
        giftTimer = view.findViewById(GWInflaterUtils.getControl(getActivity(),"timer"));
        giftExplain = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_explain"));
        giftContent = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_content"));
        giftWarning = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_warning"));
        giftExplainView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_explain_view"));
        giftWarningView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gift_warning_view"));

        receiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                receiveClick();
            }
        });
    }

    private void receiveClick(){

        if (gamePackInfo.getPackStatus().equals("1")) {
            ClipboardManager cmb = (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            cmb.setText(gamePackInfo.getPackCode());
            ToastUtil.show(getActivity(),"复制成功");
        }else {
            setPackInfo();
        }
    }

    public void setPackInfo() {
        PacksCodeProcess packCode = new PacksCodeProcess();
        packCode.setGiftId(gamePackInfo.getGiftId());
        packCode.post(handler);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Constant.PACKS_CODE_FAIL:
                    if((String) msg.obj!=null){
                        ToastUtil.show(getActivity(),(String) msg.obj);
                    }
                    break;
                case Constant.PACKS_CODE_SUCCESS:
                    gamePackInfo.setPackStatus("1");
                    setData();
                    break;
            }
        }
    };


    @Override
    public void getData() {
        gamePackInfo = (GamePackInfo) getArguments().getSerializable("packInfo");
        if (gamePackInfo != null) {
            setData();
        }
    }


    private void setData(){

        giftName.setText(gamePackInfo.getPackName());
        if (gamePackInfo.getEndTime().equals("0")) {
            giftTimer.setText("长期有效");
        } else {
            giftTimer.setText("有效期：" + gamePackInfo.getStartTime() + " 至 " + gamePackInfo.getEndTime());
        }
        if (!gamePackInfo.getDesribe().isEmpty()) {
            giftExplain.setText(gamePackInfo.getDesribe());
            giftExplainView.setVisibility(View.VISIBLE);
        } else {
            giftExplainView.setVisibility(View.GONE);
        }
        giftContent.setText(gamePackInfo.getDigest());

        if (gamePackInfo.getPackStatus().equals("1")) {
            receiveBtn.setText("复制");
            receiveBtn.setTextColor(Color.parseColor("#0FBEFD"));
            giftNumber.setText("兑换码："+gamePackInfo.getNovice());
            giftNumber.setTextColor(Color.parseColor("#54FF00"));
            receiveBtn.setBackgroundResource(GWInflaterUtils.getDrawable(getActivity(),"gw_line_button_bg"));

        }else {
            receiveBtn.setText("领取");
            receiveBtn.setTextColor(Color.parseColor("#0FBEFD"));
            giftNumber.setText("礼包剩余："+gamePackInfo.getSurplus());
            giftNumber.setTextColor(Color.parseColor("#14B6FC"));
            receiveBtn.setBackgroundResource(GWInflaterUtils.getDrawable(getActivity(),"gw_line_button_bg"));

            if(gamePackInfo.getSurplus()==0){
                receiveBtn.setEnabled(false);
                receiveBtn.setText("已领完");
                receiveBtn.setTextColor(GWInflaterUtils.getColor(getActivity(),"gw_hui"));
                receiveBtn.setBackgroundResource(GWInflaterUtils.getDrawable(getActivity(),"gw_btn_hui_bg"));
            }else{
                receiveBtn.setEnabled(true);
                receiveBtn.setText("领取");
                receiveBtn.setTextColor(GWInflaterUtils.getColor(getActivity(),"gw_bai"));
                receiveBtn.setBackgroundResource(GWInflaterUtils.getDrawable(getActivity(),"gw_login_button_bg"));
            }
        }
    }
}
